package com.umakr.ax.opt.service;

import com.google.common.collect.Maps;
import com.umakr.ax.common.model.AreaRs;
import com.umakr.ax.common.service.AreaRsService;
import com.umakr.ax.common.vo.AreaRsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author umakr
 */
@Service
public class AreaRsOptService {
    @Autowired
    AreaRsService areaRsService;

    public Object delAreaRs(Long id) {
        int errorCode = 0 ;
        String msg = "删除失败";
        Map<String,Object> data = Maps.newLinkedHashMap();
        if(id != null && !"".equals(id)){
            areaRsService.delete(id);
            errorCode = 1 ;
            msg = "删除成功";
            data.put("result",1);
        }
        return areaRsService.resultMap(data,errorCode,msg);
    }
    public Object listAreaRs(Long rsId) {
        List<AreaRsVO> list =  areaRsService.listAreaRs(rsId);
        Integer count = 0;
        for (AreaRsVO areaRsVO : list){
            areaRsVO.setKey("SELF_TEMP_ID_"+count);
            count ++ ;
        }
        Map<String,Object> data = Maps.newLinkedHashMap();
        int errorCode = 1 ;
        String msg = "加载成功";
        data.put("list",list);
        return areaRsService.resultMap(data,errorCode,msg);
    }

    public Object saveOrUpdateAreaRs(AreaRs areaRs) {
        int errorCode = 0 ;
        String msg;
        Map<String,Object> data = Maps.newLinkedHashMap();

        if(areaRs.getId() != null){
            areaRs.setUpdateTime(new Date());
        }else {
            areaRs.setCreateTime(new Date());
        }
        areaRsService.update(areaRs);
        data.put("id", areaRs.getId());
        data.put("rsId", areaRs.getRsId());
        data.put("areaId", areaRs.getAreaId());
        data.put("areaCode", areaRs.getAreaCode());
        data.put("rsFm", areaRs.getRsFm());
        data.put("createTime", areaRs.getCreateTime());
        data.put("updateTime", areaRs.getUpdateTime());
        data.put("sortNum", areaRs.getSortNum());
        errorCode = 1 ;
        msg = "操作成功";
        return areaRsService.resultMap(data,errorCode,msg);
    }
}
